package com.mef.smsplat.web.controller.sys.user;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hc24.util.IDUtil;
import com.mef.smsplat.dao.base.DataGridModel;
import com.mef.smsplat.dao.base.Page;
import com.mef.smsplat.domain.LimitInfoDomain;
import com.mef.smsplat.exception.BusinessException;
import com.mef.smsplat.service.LimitInfoService;
import com.mef.smsplat.web.controller.base.Json;

@Controller
@RequestMapping("/sys/limit")
public class LimitInfoController {
	private LimitInfoService limitInfoService;
	
	public void setLimitInfoService(LimitInfoService limitInfoService) {
		this.limitInfoService = limitInfoService;
	}
	
	@ResponseBody
	@RequestMapping("/query.sys")
	public Page<LimitInfoDomain> query(String limitName,DataGridModel dgm){
		return limitInfoService.query(limitName, dgm);
	}
	
	@ResponseBody
	@RequestMapping("/add.sys")
	public Json add(HttpSession session,LimitInfoDomain limit){
		if(limit.getLimitName()==null || "".equals(limit.getLimitName())){
			throw new BusinessException("权限名不能为空");
		}
		if(limit.getLimitUrl()==null || "".equals(limit.getLimitUrl())){
			throw new BusinessException("权限链接不能为空");
		}
		if(limit.getLimitPageClass()==null || "".equals(limit.getLimitPageClass())){
			throw new BusinessException("链接页面CLASS不能为空");
		}
		if(!(limit.getLimitSeq()+"").matches("^\\d+$")){
			throw new BusinessException("排序应为非负整数");
		}
		limit.setLimitId(IDUtil.next()+"");
		limit.setCreateTime(new Date());
		limitInfoService.save(limit);
		return new Json();
	}
	
	@ResponseBody
	@RequestMapping("/update.sys")
	public Json update(LimitInfoDomain limit){
		if(limit.getLimitName()==null || "".equals(limit.getLimitName())){
			throw new BusinessException("权限名不能为空");
		}
		if(limit.getLimitUrl()==null || "".equals(limit.getLimitUrl())){
			throw new BusinessException("权限链接不能为空");
		}
		if(limit.getLimitPageClass()==null || "".equals(limit.getLimitPageClass())){
			throw new BusinessException("链接页面CLASS不能为空");
		}
		if(!(limit.getLimitSeq()+"").matches("^\\d+$")){
			throw new BusinessException("排序应为非负整数");
		}
		limitInfoService.update(limit);
		return new Json();
	}
	
	@ResponseBody
	@RequestMapping("/del.sys")
	public Json del(String[] ids){
		limitInfoService.removeById(ids);
		return new Json();
	}
	
	
	@RequestMapping("/queryTree.sys")
	public String queryTree(HttpServletRequest request){
		List<LimitInfoDomain> list=limitInfoService.queryParent();
		Map<String, List<LimitInfoDomain>> map1=new HashMap<String,List<LimitInfoDomain>>();
		Map<String, List<LimitInfoDomain>> map2=new HashMap<String,List<LimitInfoDomain>>();
		for(int i=0;i<list.size();i++){
			LimitInfoDomain lim = list.get(i);
			List<LimitInfoDomain> listTmp = limitInfoService.querySubModule(lim.getLimitId());
			for(int j=0;j<listTmp.size();j++){
				map2.put(listTmp.get(j).getLimitId()+"", limitInfoService.querySubModule(listTmp.get(j).getLimitId()));
			}
			map1.put(lim.getLimitId(), listTmp);
		}
		
		request.setAttribute("list", list);
		request.setAttribute("map1", map1);
		request.setAttribute("map2", map2);
		
		return "/page/sys/role/role_limit";
	}
	
	
	@RequestMapping("/queryLimit.sys")
	@ResponseBody
	public List<LimitInfoDomain> queryLimit(String roleId){
		return limitInfoService.queryLimit(roleId);
	}
	
	@RequestMapping("/queryMenu.sys")
	@ResponseBody
	public List<LimitInfoDomain> queryMenu(){
		return limitInfoService.queryMenu();
	}
	
	@RequestMapping("/setLimit.sys")
	@ResponseBody
	public Json setLimit(String roleId,String[] limitId){
		limitInfoService.setLimit(roleId, limitId);
		return new Json();
	}
}
